package com.huiminpay.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.RandomUuidUtil;
import com.huiminpay.merchant.api.AppService;
import com.huiminpay.merchant.convert.AppCovert;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.entity.App;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.mapper.AppMapper;
import com.huiminpay.merchant.mapper.MerchantMapper;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AppServiceImpl implements AppService {

    @Resource
    private MerchantMapper merchantMapper;
    @Resource
    private AppMapper appMapper;

    @Override
    public AppDTO createApp(long merchantId, AppDTO appDTO) throws BusinessException {
        // 校验商户是否通过资质审核
        Merchant merchant = merchantMapper.selectById(merchantId);
        // 查询merchant是否为空
        if (merchant == null) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        // 查询merchant状态是否为2
        if ("2".equals(merchant.getAuditStatus())) {
            throw new BusinessException(CommonErrorCode.E_200003);
        }
        // 查询merchantAppName是否已经存在
        if (isExistAppName(appDTO.getAppName(), merchantId)) {
            throw new BusinessException(CommonErrorCode.E_200004);
        }
        // 保存应用信息
        appDTO.setAppId(RandomUuidUtil.getUUID());
        appDTO.setMerchantId(merchant.getId());
        App entity = AppCovert.instance.dtoToEntity(appDTO);
        appMapper.insert(entity);

        return AppCovert.instance.entityToDto(entity);
    }

    /**
     * \* 查询应用是否属于某个商户
     * \* @param appId
     * \* @param merchantId
     * \* @return
     * @param appId
     * @param merchantId
     */
    @Override
    public Boolean queryAppInMerchant(String appId, Long merchantId) {
        LambdaQueryWrapper<App> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(App::getAppId, appId).eq(App::getMerchantId, merchantId);
        Integer count = appMapper.selectCount(wrapper);
        return count > 0;
    }

    @Override
    public AppDTO getapp(String appId) {
        App app = appMapper.selectByAppId(appId);
        //app
        System.out.println(app);
        AppDTO appDTO = AppCovert.instance.entityToDto(app);
        return appDTO;
    }

    @Override
    public AppDTO queryAppByAppId(String appId) {
        LambdaQueryWrapper<App> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(App::getAppId,appId);
        App app = appMapper.selectOne(wrapper);
        System.out.println(app);
        AppDTO appDTO = AppCovert.instance.entityToDto(app);
        return appDTO;
    }

    /**
     * 查询商户下的应用列表
     * @param merchantId
     * @return
     */
    @Override
    public List<AppDTO> queryAppByMerchant(Long merchantId) throws BusinessException {
        List<App> apps = appMapper.selectList(new QueryWrapper<App>().lambda().eq(App::getMerchantId,merchantId));
        List<AppDTO> appDTOS = AppCovert.instance.listEntity2dto(apps);
        return appDTOS;
    }

    /**
     * 根据业务id查询应用
     * @param id
     * @return
     */
    @Override
    public AppDTO getAppById(String id) throws BusinessException {
        App app = appMapper.selectOne(new QueryWrapper<App>().lambda().eq(App::getAppId, id));
        return AppCovert.instance.entityToDto(app);
    }

    /**
     * 校验应用名是否已被使用
     * @param appName
     * @return
     */
    public boolean isExistAppName(String appName, long merchantId) {
        QueryWrapper<App> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(App::getAppName, appName).eq(App::getMerchantId, merchantId);
        Integer count = appMapper.selectCount(wrapper);
        return count > 0;
    }

}
